<?php
/**
 * Created by PhpStorm.
 * User: zl
 * Date: 2017/5/28
 * Time: 11:38
 */
    require_once('../Common.php');
    require_once ('PDFCreator.php');
    require_once ('../BusinessFee/BusinessFee.php');


    $approval_id = $_REQUEST['approval_id'];
    $result_array = array();


    $b_fee = new BusinessFee();
    $data = $b_fee->getBusinessFeePDFData($approval_id);
    if(!$data){
        echo json_encode(array('state'=>0, 'error'=>'DB_ERROR'));
    }

    $pdf = new PDFCreator();
    $r = $pdf->createBusinessSubmit($data);
   // echo json_encode($data);
    if(!$r){
        echo json_encode(array('state'=>0, 'error'=>'PDF_CREATE_ERROR'));
    }
    else{
        echo json_encode(array('state'=>1, 'url'=>$r));
    }
//    $conn = new SQLBase();
//
//    $data = array();
//    //查询与该ID有关的所有差旅填报信息
//    $query = "SELECT * FROM travel_fee_detail WHERE travel_approval_id = '$approval_id'";
//    $fee_result = $conn->queryData($query);
//    //获取所有相关差旅填报数据
//    if($fee_result){
//        for($i = 0; $i < $fee_result->num_rows; $i++){
//            $fee_data[$i] = $fee_result->fetch_array();
//        }
//        //获取差旅报批数据
//        $query = "SELECT travel_approval.*, allsection.section_name FROM travel_approval INNER JOIN allsection
//              WHERE travel_approval.section_ID = allsection.section_ID AND expense_claim_ID='$approval_id'";
//
//        $approval_result = $conn->queryData($query);
//        if($approval_result) {
//            $approval_data = $approval_result->fetch_array();
//            $eID = $approval_data['event_ID'];
//            if(!empty($eID)){
//                //获取大案要案名字
//                $q = "SELECT case_name FROM major_case WHERE Id='$eID'";
//                $r = $conn->queryData($q);
//                if($r){
//                    $case_name = $r->fetch_array()['case_name'];
//                }
//                else{
//                    $case_name = '';
//                }
//            }
//            else{
//                $case_name = '';
//            }
//            $approval_data['case_name']=$case_name;
//            //获取用户信息
//            $user_id = $approval_data['write_ID'];
//            $query = "SELECT * FROM user WHERE user_ID='$user_id'";
//            $user_result = $conn->queryData($query);
//            if($user_result){
//                $user_data = $user_result->fetch_array();
//                //获取报销状态信息
//                $app_state = new ApproveState();
//                $state_data = $app_state->getTravelState($approval_id);
//                if($state_data){
//                    $url = createPDF($user_data, $approval_data, $fee_data, $state_data);
//                    if(!$url){
//                        $result_array['state'] = 0;
//                        $result_array['state_error'] = 'PDF_ERROR';
//                    }
//                    else{
//                        $result_array['state'] = 1;
//                        $result_array['state_error'] = '';
//                        $result_array['url'] = $url;
//                    }
//                }
//                else{
//                    $result_array['state'] = 0;
//                    $result_array['state_error'] = "DB_ERROR";
//                }
//            }
//            else{
//                $result_array['state'] = 0;
//                $result_array['state_error'] = "DB_ERROR";
//            }
//        }
//        else {
//            $result_array['state'] = 0;
//            $result_array['error'] = "DB_ERROR";
//        }
//    }
//    else{
//        $result_array['state'] = 0;
//        $result_array['error'] = "DB_ERROR";
//    }
//
//    echo json_encode($result_array);
//
//
//    function createPDF($userData, $approval_data, $feeData, $state_data){
//        $data = array('name'=>$userData['user_name'], 'department_name'=>$approval_data['section_name'],
//            'business_type'=>getBusinessType($approval_data['travel_type']), 'case_name'=>$approval_data['case_name'],
//            'bankcard'=>$userData['card_number'], 'userID'=>$userData['user_ID'], 'approval_id'=>$approval_data['expense_claim_ID'],
//            'section_manager_ID'=>$state_data['section_manager_ID'], 'section_leader_ID'=>$state_data['section_leader_ID'], 'leader_ID'=>$state_data['leader_ID']);
//        $data['apply_detail'] = array();
//        $total_fee = 0;
//        $official_fee = 0;
//        for($i = 0; $i < count($feeData); $i++){
//            $data['apply_detail'][$i] =array(
//                'depart_time'=>$feeData[$i]['start_time'],
//                'depart_city'=>$feeData[$i]['start_dest'],
//                'arrive_time'=>$feeData[$i]['end_time'],
//                'arrive_city'=>$feeData[$i]['end_dest'],
//                'car_fee'=>$feeData[$i]['car_fee'],
//                'boat_fee'=>$feeData[$i]['boat_fee'],
//                'flight_fee'=>$feeData[$i]['flight_fee'],
//                'total_days'=>$feeData[$i]['road_days'] + $feeData[$i]['stay_days'],
//                'hotel_fee'=>$feeData[$i]['hotel_fee'],
//                'meal_fee'=>$feeData[$i]['food_fee'],
//                'extra_fee'=>$feeData[$i]['inci_fee'],
//                'other_fee'=>$feeData[$i]['other_fee']);
//            $total_fee += $feeData[$i]['total_fee'];
//            $official_fee += $feeData[$i]['official_fee'];
//        }
//        $data['official_fee'] = $official_fee;
//        $data['total_fee'] = $total_fee;
//        //echo json_encode($data);
//        $pdf = new PDFCreator();
//        return $pdf->createBusinessSubmit($data);
//
//    }
//
//    function getBusinessType($type){
//        switch($type){
//            case 1:
//                return '业务出差';
//            case 2:
//                return '大案要案';
//            case 3:
//                return '会议';
//            case 4:
//                return '培训';
//        }
//
//    }









?>